﻿/*
 *	(*^。^*) TFrameWork License (*^。^*)
 */

/*
 *  Title :
 *
 *  Description :
 *
 *
 *  Date :
 *
 *  Author : DengPing
 *
 *  Modify :
 */
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace TFrameWork.Module.ActionEditor
{
    public static class ActionEditorUtility
    {
        private static Dictionary<string, VisualTreeAsset> visualTreeAssetCache = new Dictionary<string, VisualTreeAsset>();

        private static string imageRootPath;
        
        public static T GetStyleSheet<T>(string name) where T : VisualTreeAsset
        {
            return GetAsset<T>("t:StyleSheet", name);
        }

        private static T GetAsset<T>(string type, string name) where T : VisualTreeAsset
        {
            T visualTree = null;
            if (visualTreeAssetCache.ContainsKey(name))
            {
                visualTree = visualTreeAssetCache[name] as T;
            }
            else
            {
                foreach (var guid in AssetDatabase.FindAssets(type))
                {
                    string path = AssetDatabase.GUIDToAssetPath(guid);
                    if (path.Contains(name))
                    {
                        visualTree = AssetDatabase.LoadAssetAtPath<T>(path);
                        visualTreeAssetCache[name] = visualTree;
                        break;
                    }
                }
            }

            return visualTree;
        }

        public static string GetAssetPath(string type, string name)
        {
            string res = string.Empty;
            foreach (var guid in AssetDatabase.FindAssets(type))
            {
                string path = AssetDatabase.GUIDToAssetPath(guid);
                if (path.Contains(name))
                {
                    res = path;
                    break;
                }
            }

            return res;
        }

        public static string GetImageRootPath()
        {
            if (string.IsNullOrEmpty(imageRootPath))
            {
                var assetPath = ActionEditorUtility.GetAssetPath("t:texture", "ActionEditorIcon.png");
                imageRootPath = assetPath.Replace("ActionEditorIcon.png", "");
            }
            return imageRootPath;
        }


        /// <summary>
        /// 查找子对象
        /// </summary>
        /// <returns></returns>
        public static Transform FindChildTransformByNameIndex(Transform parent, string name)
        {
            if (parent == null || string.IsNullOrEmpty(name)) return null;
            
            Transform findTrans = null;
            var trans = parent.GetComponentsInChildren<Transform>();
            foreach (Transform t in trans)
            {
                if (t.name == name)
                {
                    findTrans = t;
                }
            }
            if (findTrans == null)
            {
                Debug.Log("没有找到对应的节点 parent " + parent.name + "   findName:" + name
                );
            }

            return findTrans;
        }
        
    }
}
